home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Commun⁄Network / Mac⁄gnuucp / Source / uumisc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-08  |  9.0 KB  |  358 lines  |  [TEXT/KAHL]

  1. /*
  2.  * @(#)uumisc.c 1.6 87/09/29    Copyright 1987 Free Software Foundation, Inc.
  3.  *
  4.  * Copying and use of this program are controlled by the terms of the
  5.  * GNU Emacs General Public License.
  6.  *
  7.  * uumisc.c
  8.  *
  9.  * Miscellaneous uucp variables and routines which are used by several
  10.  * uucp main programs.
  11.  */
  12. #include "includes.h"        /* System include files, system dependent */
  13. #include "uucp.h"        /* Uucp definitions and parameters */
  14.  
  15. char    Myname[MAX_HOST];    /* Our uucp hostname, set from usenet.ctl */
  16. char    Mynamealias[MAX_HOST];    /* Our uucp hostname, set from usenet.ctl */
  17. char    Forwarder[MAX_HOST];
  18. char    host_name[MAX_HOST] = "gnuucp";    /* Other guy's host name */
  19. char    *uuControl;        /* Control file name */
  20. char    who[NAMESIZE] = "-";    /* Who sent us a file */
  21. char    *Spool;            /* Spool directory root name */
  22. char    *Alias;            /* Directory where aliases are kept */
  23. char    *Loginfile;        /* File to log calls to us */
  24. char    *Sysfile;        /* L.sys file */
  25. char    *Userfile;        /* USERFILE */
  26. char    *Pubdir;        /* Public Directory */
  27. char    *Logfile;        /* Logging file */
  28. Boolean AppendLogfile = FALSE;  /* Continously add to log file over sessions */
  29. char    *MLogfile;        /* Logging file for mail */
  30. char    *Username;        /* Username for single user machine */
  31. char    *Timezone;        /* Timezone string ("EDT", "PST"....) */
  32. char    *UUCPlogname;    /* Login name for UUCP */
  33. char    *UUCPpasswd;    /* Password for UUCP */
  34. char    *Mail;            /* Directory where mail is delivered */
  35. long    ByteTimeout;    /* Lenght of time xgetc waits for a char (in secs) */
  36. long    ConnectWait;    /* Max time we wait after dialing */
  37.  
  38. int    debug = -1;        /* -1 indicates ctl file should set it */
  39. int    ourpid = 0;        /* Our process ID */
  40. int    logfd;            /* file desc of uucp logfile */
  41.  
  42. struct port *ports, *porttail;    /* Chain of serial port definitions */
  43.  
  44. #ifdef VMS
  45. void *malloc();
  46. #else
  47. #ifndef MAC
  48. char    *malloc();
  49. #endif
  50. #endif
  51. char    *gimmestring();        /* Forward declaration */
  52.  
  53. /*
  54.  * Read the control file and grab a few parameters.
  55.  */
  56. extern int apref;
  57.  
  58. read_params(ctl)
  59.     char *ctl;
  60. {
  61.     FILE    *fd;
  62.     char    buf[MAX_CTLLINE];
  63.     register struct port *newport;
  64.     register char *cmd;
  65.     register char    *p;
  66.     int        vRefNumTmp;
  67.     GetVol(0L, &vRefNumTmp);
  68.     SetVol(0L, apref);
  69.     if (ctl) {
  70.         /* Remove any possible setuid if user spec'd a control file */
  71.         /* FIXME: this is system dependent....sigh
  72.         setuid(getuid()); */
  73.     } else {
  74.         /* Use the system default control file */
  75.         ctl = sysdep_control;
  76.     }
  77.     uuControl = ctl;
  78.     if (! (fd = fopen(ctl, "r"))) {
  79.         printf("Can't find configuration file %s\n", ctl);
  80.         SetVol(0L, vRefNumTmp);
  81.           return FAIL;
  82.     }
  83.  
  84.     /* find path to inbound news */
  85.     Spool = "";
  86.     Alias = "";
  87.     Mail = "";            
  88.     Sysfile = "";
  89.     Username = "";
  90.     Userfile = "";
  91.     Pubdir = "";
  92.     Logfile = "";
  93.     Loginfile = "";
  94.     MLogfile = "";
  95.     UUCPlogname = "";
  96.     UUCPpasswd = "";
  97.     Timezone = "";
  98.     strcpy(Myname, "");
  99.     strcpy(Mynamealias, "");
  100.     strcpy(Forwarder, "");
  101.     ByteTimeout = BYTE_TIMEOUT;
  102.     ConnectWait = CONNECT_WAIT;
  103.     while ((long)NULL != (long)fgets(buf, sizeof buf, fd)) {
  104.  
  105.         cmd = strtok(buf, CTL_DELIM);
  106.         if (!cmd) continue;        /* Skip blank lines */
  107.         for (p = cmd; *p; p++)
  108.             if (isupper(*p)) *p = tolower(*p);
  109.  
  110.         /* Now figure out which one it is */
  111.  
  112.         if (strcmp(cmd, "alias") == 0) {
  113.             Alias = gimmefilestring();
  114.         } else if (strcmp(cmd, "spool") == 0) {
  115.             Spool = gimmefilestring();
  116.         } else if (strcmp(cmd, "sysfile") == 0) {
  117.             Sysfile = gimmefilestring();
  118.         } else if (strcmp(cmd, "username") == 0) {
  119.             Username = gimmestring();
  120.         } else if (strcmp(cmd, "uucplogname") == 0) {
  121.             UUCPlogname = gimmestring();
  122.         } else if (strcmp(cmd, "uucppasswd") == 0) {
  123.             UUCPpasswd = gimmestring();
  124.         } else if (strcmp(cmd, "mail") == 0) {
  125.             Mail = gimmefilestring();
  126.         } else if (strcmp(cmd, "userfile") == 0) {
  127.             Userfile = gimmefilestring();
  128.         } else if (strcmp(cmd, "pubdir") == 0) {
  129.             Pubdir = gimmefilestring();
  130.         } else if (strcmp(cmd, "loginfile") == 0) {
  131.             Loginfile = gimmefilestring();
  132.         } else if (strcmp(cmd, "logfile") == 0) {
  133.             Logfile = gimmefilestring();
  134.         } else if (strcmp(cmd, "appendlogfile") == 0) {
  135.             {
  136.                 char *applog;
  137.                 applog = gimmestring();
  138.                 {
  139.                     if (applog != NULL && (applog[0] == 'Y' || applog[0] == 'y'))
  140.                         AppendLogfile = TRUE;
  141.                         else
  142.                         AppendLogfile = FALSE;
  143.                 free(applog);
  144.                 }
  145.         }
  146.         } else if (strcmp(cmd, "timezone") == 0) {
  147.             Timezone = gimmestring();
  148.         } else if (strcmp(cmd, "mlogfile") == 0) {
  149.             MLogfile = gimmefilestring();
  150.         } else if (strcmp(cmd, "nodename") == 0) {
  151.             strcpy(Myname, strtok((char *)NULL, CTL_DELIM) ) ;
  152.         } else if (strcmp(cmd, "nodenamealias") == 0) {
  153.             strcpy(Mynamealias, strtok((char *)NULL, CTL_DELIM) ) ;
  154.         } else if (strcmp(cmd, "forwarder") == 0) {
  155.             strcpy(Forwarder, strtok((char *)NULL, CTL_DELIM) ) ;
  156.         }
  157.           else if (strcmp(cmd, "debug") == 0) {
  158.             if (debug < 0) {
  159.                 debug = atoi(strtok((char *)NULL, CTL_DELIM));
  160.             }
  161.         }
  162.           else if (strcmp(cmd, "bytetimeout") == 0) {
  163.                 {
  164.                     ByteTimeout = atoi(strtok((char *)NULL, CTL_DELIM));
  165.             }
  166.         }
  167.           else if (strcmp(cmd, "connectwait") == 0) {
  168.                 {
  169.                     ConnectWait = atoi(strtok((char *)NULL, CTL_DELIM));
  170.             }
  171.         } else if (strcmp(cmd, "port") == 0) {
  172.             newport = (struct port *)malloc(sizeof *newport);
  173.             p = strtok((char *)NULL, CTL_DELIM);
  174.             strcpy(newport->portname, p);
  175.             p = strtok((char *)NULL, CTL_DELIM);
  176.             strcpy(newport->modemname, p);
  177.             p = strtok((char *)NULL, CTL_DELIM);
  178.             strcpy(newport->devname, p);
  179.             p = strtok((char *)NULL, CTL_DELIM);
  180.             newport->baud = atol(p);
  181.             /* Add it to the end of the chain */
  182.             newport->chain = (struct port *)0;
  183.             if (ports)
  184.                 porttail->chain = newport;
  185.             else
  186.                 ports = newport;
  187.             porttail = newport;
  188.         }
  189.     }
  190.  
  191.     fclose(fd);
  192.     SetVol(0L, vRefNumTmp);
  193.     return SUCCESS;
  194. }
  195.  
  196. /* Scan out next arg as a string, return it or NULL if none there. */
  197. char *
  198. gimmestring()
  199. {
  200.     register char *p, *q;
  201.     p = strtok((char *)NULL, CTL_DELIM);
  202.     if (p == NULL)
  203.         return p;
  204.     q = (char *)malloc(strlen(p)+1);
  205.     strcpy(q, p);
  206.     return q;
  207. }
  208.  
  209. char *
  210. gimmefilestring()
  211. {
  212.     register char *p, *q;
  213.     long len;
  214.     p = strtok((char *)NULL, FILE_DELIM);
  215.     if (p == NULL)
  216.         return p;
  217.     for (;*p == ' ';p++); /* Skip whitespace at beginning */
  218.     len = strlen(p);
  219.     for (;p[len-1] == ' ';p[len-1] = '\0'); /* Skip whitespace at end */
  220.     q = (char *)malloc(len+1);
  221.     strcpy(q, p);
  222.     return q;
  223. }
  224.  
  225. /*
  226.  * Scan out an "argv" from a blank-separated string.
  227.  *
  228.  * Result is argc, or -1 if there are too many args.
  229.  */
  230. int
  231. getargs(string, argv, maxargc)
  232.     char    *string;
  233.     char    **argv;
  234.     int    maxargc;
  235. {
  236.     int    argc = 0;
  237.  
  238.     while (*string == ' ') string++;
  239.  
  240.     for (; *string; ) {
  241.         argv[argc++] = string;        /* Record position */
  242.         if (argc >= maxargc) return -1;
  243.         while (*string && *string != ' ') string++;
  244.         if (*string == '\0') break;
  245.         *string++ = '\0';        /* Null terminate it */
  246.         while (*string == ' ') string++;
  247.     }
  248.  
  249.     argv[argc] = (char *)0;            /* Null after last one */
  250.     return argc;
  251. }
  252.  
  253. /*
  254.  * Log file writing subroutine.
  255.  *
  256.  * Makes incredibly ugly log entries that look *just like* Unix uucp's
  257.  * incredibly ugly log entries.
  258.  *
  259.  * Once we don't care about compatability, we should do this much better.
  260.  */
  261. logit(one, two)
  262.     char *one, *two;
  263. {
  264.     char logbuf[(NAMESIZE*4)+SLOP+50];    /* Temp buffer for logs */
  265.     int len;
  266.     int open_modes;
  267.     if (logfd <= 0) {
  268.         /* Open the log file if not already open */
  269.         if (Logfile == 0 || *Logfile == 0)
  270.             return;        /* If none specified, no logging. */
  271.         open_modes = O_CREAT|O_WRONLY|O_TEXT;
  272.         if (AppendLogfile == TRUE)
  273.             open_modes = open_modes | O_APPEND;
  274.             else
  275.             open_modes = open_modes | O_TRUNC;
  276.         logfd = open(Logfile, open_modes);
  277.         if (logfd < 0) {
  278.             if (DEBUG_LEVEL(0)) perror(Logfile);
  279.             exit(EXIT_ERR);
  280.         }
  281.     }
  282.  
  283.     sprintf(logbuf, "%s %s (%s) %s (%s)\n",
  284.         who, host_name, time_and_pid(), one, two);
  285.  
  286.     DEBUG(0, "%s", logbuf);
  287.  
  288.     len = strlen(logbuf);
  289.     if (len != write(logfd, logbuf, len)) {
  290.         if (DEBUG_LEVEL(0)) {
  291.             fprintf(stderr, "Can't write to log, terminating!\n");
  292.             perror(Logfile);
  293.         }
  294.         exit(EXIT_ERR);        /* Terminate if we can't log */
  295.     }
  296.  
  297. #ifdef LOGCLOSE
  298.     if (close(logfd) != 0) {
  299.         if (DEBUG_LEVEL(0)) {
  300.             fprintf(stderr, "Can't close log, terminating!\n");
  301.             perror(Logfile);
  302.         }
  303.         exit(EXIT_ERR);        /* Terminate if we can't log */
  304.     }
  305.     logfd = -1;
  306. #endif
  307. }
  308.  
  309. mlogit(one, two)
  310.     char *one, *two;
  311. {
  312.     char logbuf[(NAMESIZE*4)+SLOP+50];    /* Temp buffer for logs */
  313.     int len;
  314.  
  315.     if (logfd <= 0) {
  316.         /* Open the log file if not already open */
  317.         if (Logfile == 0 || *Logfile == 0)
  318.             return;        /* If none specified, no logging. */
  319.         logfd = open(MLogfile, O_CREAT|O_WRONLY|O_APPEND|O_TEXT);
  320.         if (logfd < 0) {
  321.             if (DEBUG_LEVEL(0)) perror(MLogfile);
  322.             exit(EXIT_ERR);
  323.         }
  324.     }
  325.  
  326.     sprintf(logbuf, "%s %s (%s) %s (%s)\n",
  327.         who, host_name, time_and_pid(), one, two);
  328.  
  329.     DEBUG(0, "%s", logbuf);
  330.  
  331.     len = strlen(logbuf);
  332.     if (len != write(logfd, logbuf, len)) {
  333.         if (DEBUG_LEVEL(0)) {
  334.             fprintf(stderr, "Can't write to log, terminating!\n");
  335.             perror(MLogfile);
  336.         }
  337.         exit(EXIT_ERR);        /* Terminate if we can't log */
  338.     }
  339.  
  340. #ifdef LOGCLOSE
  341.     if (close(logfd) != 0) {
  342.         if (DEBUG_LEVEL(0)) {
  343.             fprintf(stderr, "Can't close log, terminating!\n");
  344.             perror(MLogfile);
  345.         }
  346.         exit(EXIT_ERR);        /* Terminate if we can't log */
  347.     }
  348.     logfd = -1;
  349. #endif
  350. }
  351.  
  352. void
  353. cuserid(str)
  354. char *str;
  355. {
  356.     strcpy(str, Username);
  357.     }
  358.